package cas.ihep.fs.file;

import cas.ihep.fs.UnifiedAbstractFileSystem;
import cas.ihep.fs.UnifiedFile;
import cas.ihep.fs.UnifiedFileSystem;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

public class LocalFileSystem implements UnifiedAbstractFileSystem {
    @Override
    public UnifiedFile open(URI path, UnifiedFileSystem.OpenOptions options) {
        return new LocalFile(path);
    }

    @Override
    public UnifiedFile create(URI path, UnifiedFileSystem.CreateOptions options) {
        return new LocalFile(path);
    }

    @Override
    public InputStream openStream(URI path, UnifiedFileSystem.OpenOptions options) throws IOException {
        return new LocalFile(path).openStream();
    }

    @Override
    public OutputStream createStream(URI path, UnifiedFileSystem.CreateOptions options) throws IOException {
        LocalFile f=new LocalFile(path);
        f.create(options);
        return f.createStream();
    }

    @Override
    public void persist(URI path) {

    }

    @Override
    public void persist(UnifiedFile file) {

    }

    @Override
    public void free(URI path) {

    }

    @Override
    public void free(UnifiedFile file) {

    }

    @Override
    public void delete(URI path) {
        //noinspection ResultOfMethodCallIgnored
        new File(path.getPath()).delete();
    }

    @Override
    public void delete(UnifiedFile file) {
        if(file instanceof LocalFile){
            //noinspection ResultOfMethodCallIgnored
            ((LocalFile)file).file.delete();
        }
    }

    @Override
    public void mkdir(URI path, UnifiedFileSystem.CreateOptions options) throws IOException {
        //noinspection ResultOfMethodCallIgnored
        new File(path.getPath()).mkdir();
    }

    @Override
    public void close() {

    }
}
